//
//  EvaluateTextCell.m
//  QoilPublic
//
//  Created by SunChao on 17/9/13.
//  Copyright © 2017年 杨凯. All rights reserved.
//
#define MAX_LIMIT_NUMS 200

#import "EvaluateTextCell.h"

@implementation EvaluateTextCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    _contentTextView.delegate = self;
    _contentTextView.font = [UIFont systemFontOfSize:15];
    _contentTextView.textColor = [UIColor grayColor];
    _contentTextView.layer.cornerRadius = 5.0f;
    _contentTextView.layer.masksToBounds = YES;
    _contentTextView.layer.borderWidth = 0.5;
    _contentTextView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.countNumLabel.font = [UIFont systemFontOfSize:13];
    self.countNumLabel.textColor = [UIColor blackColor];
    UIToolbar *bar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, kScreenWidth,44)];
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 60, 7,50, 30)];
    [button setTitle:@"完成"forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(handleFinishBtn) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    UIBarButtonItem *flexibleitem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemFlexibleSpace) target:self action:nil];
    NSArray *items = @[flexibleitem,flexibleitem,item];
    [bar setItems:items animated:YES];
    
    _contentTextView.inputAccessoryView = bar;
    
    
}
- (void)handleFinishBtn{
    
    [_contentTextView resignFirstResponder];
}


- (void)completeAction:(UIButton *)button{
    
    [self endEditing:YES];
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(gainStarWithText:)]){
        
        [self.delegate gainStarWithText:_contentTextView.text];
    }
    
    
}
#pragma mark -- UITextFieldDelegate

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    if ([_contentTextView isFirstResponder])
    {
        [_contentTextView resignFirstResponder];
    }
}

#pragma mark - UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
 replacementText:(NSString *)text
{
    
    if ([text isEqualToString:@"\n"]) {
        
        [textView resignFirstResponder];
        return NO;
    }
    NSString *comcatstr = [textView.text stringByReplacingCharactersInRange:range withString:text];
    
    NSInteger caninputlen = MAX_LIMIT_NUMS - comcatstr.length;
    
    if (caninputlen >= 0)
    {
        return YES;
    }
    else
    {
        NSInteger len = text.length + caninputlen;
        //防止当text.length + caninputlen < 0时，使得rg.length为一个非法最大正数出错
        NSRange rg = {0,MAX(len,0)};
        
        if (rg.length > 0)
        {
            NSString *s = [text substringWithRange:rg];
            
            [textView setText:[textView.text stringByReplacingCharactersInRange:range withString:s]];
            self.countNumLabel.text = @"可输入200/200字";
        }
        return NO;
    }
    
}

- (void)textViewDidChange:(UITextView *)textView
{
    
//    if (textView.text.length == 0) {
//        _placeHolderLabel.text = @"建议";
//    }else{
//        _placeHolderLabel.text = @"";
//    }
    
    NSString  *nsTextContent = textView.text;
    NSInteger existTextNum = nsTextContent.length;
    
    if (existTextNum > MAX_LIMIT_NUMS)
    {
        //截取到最大位置的字符
        NSString *s = [nsTextContent substringToIndex:MAX_LIMIT_NUMS];
        
        [textView setText:s];
    }
    //不让显示负数
    self.countNumLabel.text = [NSString stringWithFormat:@"可输入%ld/%d字",MAX(0,MAX_LIMIT_NUMS - existTextNum),MAX_LIMIT_NUMS];
}
- (void)textViewDidBeginEditing:(UITextView *)textView{
    if ([textView.text isEqualToString:@"请写下您的意见和建议，或遇到的问题，我们会尽快处理(10~200字以内)"]) {
        textView.text = @"";
        textView.textColor = [UIColor blackColor];
//        self.btnInput.enabled = NO;
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView{
    [self.contentTextView resignFirstResponder];
    if ([self.contentTextView.text isEqualToString:@""]) {
        self.contentTextView.text = @"请写下您的意见和建议，或遇到的问题，我们会尽快处理(10~200字以内)";
        textView.textColor = [UIColor grayColor];
//        self.btnInput.enabled = NO;
    }
    if (self.contentTextView.text.length > 200 || self.contentTextView.text.length < 10) {
        if (self.contentTextView.text.length > 200) {
            self.contentTextView.text = [self.contentTextView.text substringToIndex:200];

        }else{

            
        }
    }else{
        
    }
}


@end
